define(
	[
	appBasePath + '/scripts/module.js',
	],
	function (substanceModule) {

    'use strict';

    substanceModule.controller('Substance.GoodsListController', [
        '$scope',
        '$state',
        '$stateParams',
        'gbits.websns.substance.config',
        'Substance.GoodsService',
        'Substance.SettingService',
        '$timeout',
        function($scope, $state, $stateParams, config, goodsService, settingService, $timeout) {
        	$scope.list = [];        	
        	$scope.queryStr = "";
        	$scope.page = {
        			currentPage: $stateParams.currentPage || 1
        	};
        	$scope.storeInCount = '';
        	// 查询
        	$scope.query = function() {
        		if (!$scope.queryStr) {
        			$scope.errorMsg({message: "查询条件不能为空"});
        			return false;
        		}
        		$timeout(function(){
        			$scope.page.currentPage = 1;
            		$scope.loadData();
        		});
        	}
        	
        	var ld = function loadData() {
        		$scope.list = [];
        		goodsService.list($scope.queryStr, $scope.page.currentPage).then(function(data) {
        			$timeout(function(){
        				$scope.list = data.data;
            			$scope.page = data;
            			$scope.page.prePage = 1;
            			$scope.page.nextPage = data.lastPage;
            			if (data.currentPage > 1) {
            				$scope.page.prePage = data.currentPage - 1;
            			}
            			if (data.currentPage < $scope.page.lastPage - 1) {
            				$scope.page.nextPage = data.currentPage + 1;
            			}
            			$scope.page.showPages = [];
            			for (var i = data.currentPage - 5; i<=data.lastPage; i++) {
            				if (i>0) {
            					$scope.page.showPages.push(i);
            				}
            				if ($scope.page.showPages.length > 10) {
            					break;
            				}
            			}
        			})
        			
        		});
        	}
        	
        	$scope.openStoreInModal = function(id) {
        		$timeout(function(){
        			$scope.storeInCount = '';
        			$(".background-panel").css("display","block");
        			$("#goods-storein").attr("gid", id).css("display","block");
        			$(".cago-close").bind('click', function(){
        				$(".background-panel").css("display","none");
        				$(".cago-add").css("display","none");
        			});
        		});
        	}
        	
        	$scope.openSetAdminModal = function() {
        		settingService.list({currentPage: $scope.page.currentPage}).then(function(data) {
        			$timeout(function(){
        				
        				var approve = $("#approve_options").html('');
        				var admin = $("#admin_options").html('');
        				$(data.data).each(function(i){
        					var u = data.data[i];
        					var o1 = $("<option></option>").val(u.userId).text(u.userName);
        					var o2 = $("<option></option>").val(u.userId).text(u.userName);
        					if (u.adminUser) {
        						o1.attr("selected", 'selected');
        					}
        					if (u.approveUser) {
        						o2.attr("selected", 'selected');
        					}
        					o1.appendTo(admin);
        					o2.appendTo(approve);
        				});
            			
        				if (!$scope.adminSelect) {
        					$scope.adminSelect = $(".js-example-basic-multiple1").select2({
                			    placeholder:"请选择管理员"
                			});
        				}
        				if (!$scope.approveSelect) {
        					$scope.approveSelect = $(".js-example-basic-multiple2").select2({
                			    placeholder:"请选择审批人"
                			});
        				}
        				
            			$("#setadmin").show();
            			$("#setadmin .glyphicon-remove").bind("click",function(){
            			    $(".admin-add").hide();
            			});
            			  $("#setadmin .btn-concel").bind("click",function(){
            			    $("#setadmin").hide();
            			  });
        			})
        			
        		});
        	}
        	// 保存
        	$scope.saveUsers = function() {
        		var admins = $("#admin_select").val();
        		var approves = $("#approve_select").val();
        		$timeout(function(){
        			settingService.setManagerUser({admins: admins.join(','), approves: approves.join(',')}).then(function(data) {
        				if (data.success) {
        					$("#setadmin").hide();
        				} else {
        					$scope.errorMsg(data);
        				}
        			});
        		});
        		
        	}
        	
        	$scope.openUpdateStatus = function(id, status) {
        		$timeout(function(){
        			var result = 1;
        			if (status == 1) {
        				$("#goods-status .text").text('确定停用吗');
        				result = 2;
        			} else {
        				$("#goods-status .text").text('确定启用吗');
        			}
        			$(".background-panel").css("display","block");
        			$("#goods-status").attr("gid", id).attr("gstatus", result).css("display","block");
        			$("#goods-status .cago-close").bind('click', function(){
        				$(".background-panel").css("display","none");
        				$("#goods-status").css("display","none");
        			});
        		});
        	}
        	
        	$scope.updateStatus = function() {
        		var gid = $("#goods-status").attr('gid'), status = $("#goods-status").attr('gstatus');
        		goodsService.updateGoodsStatus(gid, status).then(function(data){
    				if (data.success) {
    					$(".background-panel").css("display","none");
        				$("#goods-status").css("display","none");
        				
        				for (var i=0; i<$scope.list.length; i++) {
    						var g = $scope.list[i];
    						if (g.id == gid) {
    							$scope.list[i] = data.data;
    							break;
    						}
    					}
    				} else {
    					$scope.errorMsg(data);
    				}
    			});
        	}
         	
        	$scope.storein = function() {
        		var gid = $("#goods-storein").attr('gid')
    			if (!$scope.storeInCount) {
    				return;
    			}
    			goodsService.storein(gid, $scope.storeInCount).then(function(data){
    				if (data.success) {
    					$timeout(function(){
        					for (var i=0; i<$scope.list.length; i++) {
        						var g = $scope.list[i];
        						if (g.id == gid) {
        							$scope.list[i].storeCount = data.data.storeCount;
        							break;
        						}
        					}
        					$(".background-panel").css("display","none");
        	  		    	$("#goods-storein").css("display","none");
    	        		});
    					
    				} else {
    					$scope.errorMsg(data);
    				}
    			});
        		
        	}
        	
        	
        	
        	$scope.loadData = ld;
        	$scope.loadData();
        }
    ]);
});